package com.gfxy.modules.classtime.service.impl;

import com.gfxy.modules.classtime.entity.ClasstimeReport;
import com.gfxy.modules.classtime.service.IClasstimeReportService;
import com.gfxy.modules.classtime.service.IClasstimeScoreService;
import com.gfxy.modules.classtime.service.IClasstimeUploadService;
import com.gfxy.modules.excel.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.*;

@Slf4j
@Service
public class ClasstimeUploadServiceImpl implements IClasstimeUploadService {

    @Value(value = "${jeecg.path.upload}")
    private String uploadpath;

    private int cellNum = 10;

    @Autowired
    private IClasstimeReportService classtimeReportService;

    @Autowired
    private IClasstimeScoreService classtimeScoreService;

    @Override
    public void importData(String year, String savePath) {
        try{
            log.info("开始导入{}年的课时数据， 课时数据文件为:{}", year,savePath);
            String ctxPath = uploadpath;
            File file = new File(ctxPath + File.separator + savePath );

            InputStream ips = new FileInputStream(file);
            Workbook workbook= WorkbookFactory.create(ips);
            Sheet sheet = workbook.getSheetAt(0);
            Map<String, List<ClasstimeReport>> reportMap = new HashMap<>();

            Row titleRow = sheet.getRow(1);
            for(int cellI = 0 ; cellI<titleRow.getLastCellNum(); cellI++){
                String monthStr = ExcelUtil.formatCell(titleRow.getCell(cellI));
                if(!monthStr.isEmpty()){
                    System.out.println(cellI + ":" + monthStr);
                    List<ClasstimeReport> classtimeReportList = new ArrayList<>();
                    for(int i = 3; i< sheet.getLastRowNum() ; i++){
                        Row row = sheet.getRow(i);

                        String teacherCode = ExcelUtil.formatCell(row.getCell(cellI + 1));

                        if(teacherCode!=null || !teacherCode.isEmpty() || !teacherCode.equals("null")){
                            ClasstimeReport report = new ClasstimeReport();
                            report.setTeacherCode(teacherCode.trim());
                            String totalClassTime =ExcelUtil.formatCell(row.getCell(cellI + 3));
                            String lecture = ExcelUtil.formatCell(row.getCell(cellI + 4));
                            String courseDesign = ExcelUtil.formatCell(row.getCell(cellI + 5));
                            String practical = ExcelUtil.formatCell(row.getCell(cellI + 6));
                            String graduateTutorial = ExcelUtil.formatCell(row.getCell(cellI +7));
                            String graduateDefense = ExcelUtil.formatCell(row.getCell(cellI+8));
                            String other = ExcelUtil.formatCell(row.getCell(cellI+9));
                            report.setTotalClasstime(totalClassTime.isEmpty() ? 0.0 : Double.parseDouble(totalClassTime));
                            report.setLecture(lecture.isEmpty() ? 0.0 : Double.parseDouble(lecture));
                            report.setCourseDesign(courseDesign.isEmpty() ? 0.0 : Double.parseDouble(courseDesign));
                            report.setPractical(practical.isEmpty() ? 0.0 : Double.parseDouble(practical));
                            report.setGraduateTutorial(graduateTutorial.isEmpty() ? 0.0 : Double.parseDouble(graduateTutorial));
                            report.setGraduateDefense(graduateDefense.isEmpty() ? 0.0 : Double.parseDouble(graduateDefense));
                            report.setOther(other.isEmpty() ? 0.0 : Double.parseDouble(other));

                            report.setDate(year);
                            report.setMonth(monthStr);

                            classtimeReportList.add(report);
                        }

                    }
                    reportMap.put(monthStr, classtimeReportList);
                }
            }

            for (Map.Entry<String, List<ClasstimeReport>> stringListEntry : reportMap.entrySet()) {

                Map<String, Object> columnMap = new HashMap<>();
                columnMap.put("date",year);
                columnMap.put("month", stringListEntry.getKey().trim());
                log.debug("开始存储[{}]年[{}]月的课时数据， 共[{}]教师" , year, stringListEntry.getKey().trim(), stringListEntry.getValue().size());
                classtimeReportService.removeByMap(columnMap);
                classtimeReportService.saveBatch(stringListEntry.getValue());
            }
            log.debug("导入完成，开始计算积分");
            classtimeScoreService.computeClasstimePoints(year);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}
